package com.geeguo.ebuilder.client.base.security.interceptor;

import com.geeguo.ebuilder.client.base.security.model.LoginUser;
import com.geeguo.ebuilder.client.base.security.utils.AuthUtils;
import com.geeguo.ebuilder.core.security.utils.TokenHelper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.AsyncHandlerInterceptor;

public class TokenInterceptor implements AsyncHandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }
        String token = TokenHelper.getToken();
        if (StringUtils.isNotEmpty(token)) {
            LoginUser loginUser = AuthUtils.getLoginUser(token);
            if (loginUser != null) {
                AuthUtils.verifyExpireTime(loginUser);
            }
        }
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}